var picOk = 0
function upload() {
    $('[type=file]').eq(0).click();
    document.getElementById('picture').onchange = function () {

        var picture = document.getElementById('picture').files[0]
        var name = picture.name.split('.')[1]
        if (name == 'jpg' || name == 'JPG' || name == 'png' || name == 'PNG' || name == 'gif' || name == 'GIF') {
            var reader = new FileReader()
            reader.onload = function (e) {

                var IMG = "<img src='" + e.target.result + "' class='card-img-top' />";

                $("#imgyulan").html(IMG);
            }
            reader.readAsDataURL(picture);
            picOk = 1
        } else {
            alert("上传截图文件有误")
            picOk = 0
        }
    }
}

function save() {
    if (picOk == 1) {
        $("#save").attr("disabled", true);
        setTimeout(function () {
            $("#save").attr("disabled", false);
        }, 3000)
        var formData = new FormData($("#uploadForm")[0]);
        $.ajax({
            type: "POST",
            url: "/file/uploadImg",
            data: formData,
            async: true,
            contentType: false,
            processData: false,
            success: function (data) {
                // console.log(data)
                if (data.err == 0) {
                    let picUrl = data.img;
                    if ($("#remark").val() == "") {
                        $("#remark").val("略")
                    }

                    if ($("#answerA").val() == "" || $("#answerB").val() == "" || $("#answerC").val() == "" || $("#answerD").val() == "") {
                        alert("存在空选项")
                        return false;
                    } else if ($("#subjectValue").val() == "") {
                        alert("请填写试题对应分值")
                        return false;
                    } else {

                        var relAnswer = new Array();
                        for (let index = 0; index < $(".relAnswer").length; index++) {
                            console.log(relAnswer.indexOf($(".relAnswer")[index].value))
                            if (relAnswer.indexOf($(".relAnswer")[index].value) === -1) {
                                relAnswer.push($(".relAnswer")[index].value)
                            } else {
                                alert("存在相同答案重复")
                                return false;
                            }
                        }
                        var isMany = 0
                        var relAnswerStr = ""
                        console.log(relAnswer)
                        if (relAnswer.length != 1) {
                            isMany = 1
                            for (let index = 0; index < relAnswer.length; index++) {
                                relAnswerStr += relAnswer[index] + ","
                            }
                        } else {
                            isMany = 0
                            relAnswerStr += relAnswer[0]
                        }


                        console.log(relAnswerStr)

                        $.ajax({
                            type: "POST",
                            url: "/admin/addpage/updateexamworks",
                            data: {
                                id: getUrlParam("id"),
                                answerA: $("#answerA").val(),
                                answerB: $("#answerB").val(),
                                answerC: $("#answerC").val(),
                                answerD: $("#answerD").val(),
                                picUrl: picUrl,
                                relAnswer: relAnswerStr,
                                isMany: isMany,
                                subjectValue: $("#subjectValue").val(),
                                subjectNumber: $("#subjectNumber").val(),
                                remark: $("#remark").val(),
                                testWordId: $("#testWordId").val(),
                                examWordId: $("#examWordId").val(),
                            },
                            // contentType: "application/json; charset=utf-8",
                            success: function (data) {
                                console.log(data)
                                if (data.err == 0) {
                                    alert(data.msg);
                                    window.location.href = data.data.url
                                } else {
                                    alert(data.msg);
                                }
                            }
                        });
                    }


                } else {
                    alert(data.msg);
                }
            }
        });
    } else {
        let picUrl = $("#yulan").attr('src');
        if ($("#remark").val() == "") {
            $("#remark").val("略")
        }

        if ($("#answerA").val() == "" || $("#answerB").val() == "" || $("#answerC").val() == "" || $("#answerD").val() == "") {
            alert("存在空选项")
            return false;
        } else if ($("#subjectValue").val() == "") {
            alert("请填写试题对应分值")
            return false;
        } else {

            var relAnswer = new Array();
            for (let index = 0; index < $(".relAnswer").length; index++) {
                console.log(relAnswer.indexOf($(".relAnswer")[index].value))
                if (relAnswer.indexOf($(".relAnswer")[index].value) === -1) {
                    relAnswer.push($(".relAnswer")[index].value)
                } else {
                    alert("存在相同答案重复")
                    return false;
                }
            }
            var isMany = 0
            var relAnswerStr = ""
            console.log(relAnswer)
            if (relAnswer.length != 1) {
                isMany = 1
                for (let index = 0; index < relAnswer.length; index++) {
                    relAnswerStr += relAnswer[index] + ","
                }
            } else {
                isMany = 0
                relAnswerStr += relAnswer[0]
            }


            console.log(relAnswerStr)

            $.ajax({
                type: "POST",
                url: "/admin/addpage/updateexamworks",
                data: {
                    id: getUrlParam("id"),
                    answerA: $("#answerA").val(),
                    answerB: $("#answerB").val(),
                    answerC: $("#answerC").val(),
                    answerD: $("#answerD").val(),
                    picUrl: picUrl,
                    relAnswer: relAnswerStr,
                    isMany: isMany,
                    subjectValue: $("#subjectValue").val(),
                    subjectNumber: $("#subjectNumber").val(),
                    remark: $("#remark").val(),
                    testWordId: $("#testWordId").val(),
                    examWordId: $("#examWordId").val(),
                },
                success: function (data) {
                    console.log(data)
                    if (data.err == 0) {
                        alert(data.msg);
                        window.location.href = data.data.url
                    } else {
                        alert(data.msg);
                    }
                }
            });
        }
    }
}

var Answer = 1
function addAnswer() {
    if (Answer < 4) {
        let tem = `
        <div class="form-group row">
            <label for="inputState" class="col-sm-1 col-form-label">正确选项</label>
            <div class="col-sm-9">
                <select class="relAnswer form-control" required>
                    <option selected>请选择正确答案</option>
                    <option value="A">A</option>
                    <option value="B">B</option>
                    <option value="C">C</option>
                    <option value="D">D</option>
                </select>
            </div>
        </div>
        `
        $('#answerCon').append(tem)
        Answer++
    } else {
        alert("多选答案不得超过4项")
    }
}

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象  
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数   
    if (r != null) return unescape(r[2]); return null; //返回参数值  
}